{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
}}

{{#each (get @model this.fieldGroups) as |fieldGroup|}}
  {{#each-in fieldGroup as |group fields|}}
    {{#if (or (not @renderGroup) (and @renderGroup (eq group @renderGroup)))}}
      {{#if (eq group "default")}}
        {{#each fields as |attr|}}
          {{! SHOW ALL FIELDS IF CREATING A NEW MODEL }}
          {{#if (or (eq @mode "create") @model.isNew)}}
            <FormField
              data-test-field
              @attr={{attr}}
              @mode={{@mode}}
              @model={{@model}}
              @onChange={{@onChange}}
              @onKeyUp={{@onKeyUp}}
              @modelValidations={{@modelValidations}}
              @showHelpText={{@showHelpText}}
            />
          {{else}}
            {{! OTHERWISE WE'RE EDITING }}
            {{#if (or (eq attr.name "name") (attr.options.editDisabled))}}
              <ReadonlyFormField @attr={{attr}} @value={{get @model attr.name}} />
            {{else if (and @useEnableInput attr.options.sensitive)}}
              <EnableInput data-test-enable-field={{attr.name}} class="field" @attr={{attr}}>
                <FormField @attr={{attr}} @model={{@model}} @modelValidations={{@modelValidations}} @onKeyUp={{@onKeyUp}} />
              </EnableInput>
            {{else}}
              <FormField
                data-test-field
                @attr={{attr}}
                @mode={{@mode}}
                @model={{@model}}
                @onChange={{@onChange}}
                @onKeyUp={{@onKeyUp}}
                @modelValidations={{@modelValidations}}
                @showHelpText={{@showHelpText}}
              />
            {{/if}}
          {{/if}}
        {{/each}}
      {{else}}
        <ToggleButton
          @isOpen={{eq this.showGroup group}}
          @openLabel={{concat "Hide " group}}
          @closedLabel={{group}}
          @onClick={{fn this.toggleGroup group}}
          class="is-block"
          data-test-button={{group}}
        />
        {{#if (eq this.showGroup group)}}
          <div class="box is-marginless">
            {{#each fields as |attr|}}
              {{! SHOW ALL FIELDS IF CREATING A NEW MODEL }}
              {{#if (or (eq @mode "create") @model.isNew)}}
                <FormField
                  data-test-field
                  @attr={{attr}}
                  @mode={{@mode}}
                  @model={{@model}}
                  @onChange={{@onChange}}
                  @onKeyUp={{@onKeyUp}}
                  @modelValidations={{@modelValidations}}
                  @showHelpText={{@showHelpText}}
                >
                  {{#if (and (has-block "identityTokenKey") (eq attr.name "config.identity_token_key"))}}
                    {{yield to="identityTokenKey"}}
                  {{/if}}
                </FormField>
              {{else}}
                {{! OTHERWISE WE'RE EDITING }}
                {{#if (or (eq attr.name "name") (eq attr.options.editDisabled true))}}
                  <ReadonlyFormField @attr={{attr}} @value={{get @model attr.name}} />
                {{else}}
                  <FormField
                    data-test-field
                    @attr={{attr}}
                    @mode={{@mode}}
                    @model={{@model}}
                    @onChange={{@onChange}}
                    @onKeyUp={{@onKeyUp}}
                    @modelValidations={{@modelValidations}}
                    @showHelpText={{@showHelpText}}
                  />
                {{/if}}
              {{/if}}
            {{/each}}
          </div>
        {{/if}}
      {{/if}}
    {{/if}}
  {{/each-in}}
{{/each}}